home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
fish
/
001-100
/
001-025
/
005
/
joystick
/
joystick.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-17
|
8KB
|
293 lines
/*
*
* DISCLAIMER:
*
* This program is provided as a service to the programmer
* community to demonstrate one or more features of the Amiga
* personal computer. These code samples may be freely used
* for commercial or noncommercial purposes.
*
* Commodore Electronics, Ltd ("Commodore") makes no
* warranties, either expressed or implied, with respect
* to the program described herein, its quality, performance,
* merchantability, or fitness for any particular purpose.
* This program is provided "as is" and the entire risk
* as to its quality and performance is with the user.
* Should the program prove defective following its
* purchase, the user (and not the creator of the program,
* Commodore, their distributors or their retailers)
* assumes the entire cost of all necessary damages. In
* no event will Commodore be liable for direct, indirect,
* incidental or consequential damages resulting from any
* defect in the program even if it has been advised of the
* possibility of such damages. Some laws do not allow
* the exclusion or limitation of implied warranties or
* liabilities for incidental or consequential damages,
* so the above limitation or exclusion may not apply.
*
*/
/* joystick.c, shows how to read a joystick on the Amiga */
/* *********************************************************************** */
/* joystick test, for right game port on the Amiga.
Notes: The right port is used for this test because the input.device
task is busy continuously with the lefthand port, feeding input events
to intuition or console devices. If Intuition is not activated
(applications which take over the whole machine may decide not to
activate Intuition), and if no console.device is activated either,
the input.device will never activate... allowing the application
free reign to use either the left OR the right hand joystick/mouse
port. If either intuition or the console device are activated,
the lefthand port will yield, at best, every alternate input
event to an external application such as this test program.
This will undoubtedly mess up either of the two applications
and should therefore be avoided. It was ok to use the right
port in this case, since the system has no particular interest
in monitoring it.
Author: Rob Peck, 12/1/85
This code may be freely utilized in developing programs for the Amiga.
*********************************************************************** */
#include <exec/types.h>
#include <exec/devices.h>
#include <graphics/gfx.h>
#include <devices/gameport.h>
#include <devices/inputevent.h>
LONG GfxBase=0;
#define XMOVE 10
#define YMOVE 10
#define MAX(m,n) (m > n ? m : n)
#define FOREVER for(;;)
struct InputEvent *game_data; /* pointer into the returned data area
* where input event has been sent */
SHORT error;
struct IOStdReq *game_io_msg;
BYTE gamebuffer[sizeof( struct InputEvent )];
BYTE *gamebuff;
SHORT testval;
SHORT codevalue;
struct MsgPort *game_msg_port;
SHORT movesize;
extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();
SHORT codeval, timeouts;
#define IF_NOT_IDLE_TWO_MINUTES while(timeouts < 4)
main()
{
printf("Joystick Demo\n");
printf("\nPlug a Joystick Into Right Port\n");
printf("\nThen move the stick and click its buttons");
gamebuff = &gamebuffer[0];
/* point to first location in game buffer */
/* SYSTEM DEVICE COMMUNICATIONS SUPPORT SETUP ROUTINES *************** */
game_msg_port = CreatePort(0,0);
/* provide a port for the IO response */
if(game_msg_port == 0)
{
printf("\nError While Performing CreatePort");
exit(-1);
}
game_io_msg = CreateStdIO(game_msg_port);
/* make an io request block for communicating with
the gameport */
if(game_io_msg == 0)
{
printf("\nError While Performing CreateStdIO");
DeletePort(game_msg_port);
exit(-2);
}
/* ********************************************************************** */
/* OPEN THE DEVICE */
error = OpenDevice("gameport.device",1,game_io_msg,0);
/* open the device for access, unit 1 is right port */
if(error != 0)
{
printf("\nError while opening the device, exiting");
DeleteStdIO(game_io_msg);
DeletePort(game_msg_port);
exit(-3);
}
/* ********************************************************************** */
/* SET THE DEVICE TYPE */
game_data = (struct InputEvent *)gamebuffer;
/* test the joystick in this loop */
if (set_controller_type(GPCT_ABSJOYSTICK) != 0)
{
printf("\nError while trying to set GPCT_ABSJOYSTICK");
DeleteStdIO(game_io_msg);
DeletePort(game_msg_port);
exit(-4);
}
/* ********************************************************************** */
/* SET THE DEVICE TRIGGER */
if (set_controller_trigger() != 0)
{
printf("\nError while trying to set controller trigger");
DeleteStdIO(game_io_msg);
DeletePort(game_msg_port);
exit(-4);
}
/* ********************************************************************** */
/* TELL USER WHAT YOU WILL BE DOING */
printf("\nI will report: \n");
printf("\n Stick X or Y moves");
printf("\n Button presses (along with stick moves if any)");
/* ********************************************************************** */
/* SETUP THE IO MESSAGE BLOCK FOR THE ACTUAL DATA READ */
game_io_msg->io_Command = GPD_READEVENT;
/* from now on, just read input events */
game_io_msg->io_Data = (APTR)gamebuffer;
/* into the input buffer, one at a time. */
/* read-event waits for the preset conditions */
game_io_msg->io_Length = sizeof(struct InputEvent);
/* read one event each time we go back to the gameport */
game_io_msg->io_Flags = 0;
/* dont use quick io */
/* ********************************************************************** */
/* LOOP FOREVER */
FOREVER
{
game_io_msg->io_Length = sizeof(struct InputEvent);
/* read one event each time we go back to the gameport */
printf("\n Waiting For Joystick Report\n");
SendIO(game_io_msg);
WaitPort(game_msg_port);
/* this is NOT a busy wait... it is a task-sleep */
GetMsg(game_msg_port);
codevalue = game_data->ie_Code;
if(codevalue == IECODE_LBUTTON)
printf("\nFire Button pressed");
if(codevalue == (IECODE_LBUTTON + IECODE_UP_PREFIX))
printf("\nFire Button released");
which_direction();
showbugs();
}
/* PROGRAM EXIT ..... temporarily no way to get here from FOREVER */
set_controller_type(GPCT_NOCONTROLLER);
CloseDevice(game_io_msg);
DeleteStdIO(game_io_msg);
DeletePort(game_msg_port);
printf("\nExiting program... 2 minutes with no activity sensed\n1> ");
return(0);
}
int which_direction()
{
SHORT xmove, ymove;
xmove = game_data->ie_X;
ymove = game_data->ie_Y;
switch(ymove)
{
case (-1):
printf("\nForward");
break;
case (1):
printf("\nBack");
break;
default:
break;
}
switch(xmove)
{
case (-1):
printf("\nLeft");
break;
case (1):
printf("\nRight");
break;
default:
break;
}
return(0);
}
int set_controller_type(type)
SHORT type;
{
game_io_msg->io_Command = GPD_SETCTYPE;
/* set type of controller to mouse */
game_io_msg->io_Length = 1;
game_io_msg->io_Data = (APTR)gamebuff;
*gamebuff = type;
SendIO(game_io_msg);
/* set it up */
/* this command doesn't wait... returns immediately */
WaitPort(game_msg_port);
GetMsg(game_msg_port);
return((int)game_io_msg->io_Error);
}
int set_controller_trigger()
{
struct GamePortTrigger gpt;
game_io_msg->io_Command = GPD_SETTRIGGER;
game_io_msg->io_Length = sizeof(gpt);
game_io_msg->io_Data = (APTR)&gpt;
gpt.gpt_Keys = GPTF_UPKEYS+GPTF_DOWNKEYS;
gpt.gpt_Timeout = 0;
gpt.gpt_XDelta = 1;
gpt.gpt_YDelta = 1;
return(DoIO(game_io_msg));
}
showbugs()
{
struct InputEvent *e;
e = (struct InputEvent *)&gamebuffer[0];
/* where the input event gets placed */
printf("\nie_Class = %lx",e->ie_Class);
printf("\nie_SubClass = %lx",e->ie_SubClass);
printf("\nie_Code = %lx", e->ie_Code);
printf("\nie_Qualifier = %lx",e->ie_Qualifier);
printf("\nie_X = %ld", e->ie_X);
printf("\nie_Y = %ld", e->ie_Y);
printf("\nie_TimeStamp(seconds) = %lx", e->ie_TimeStamp.tv_secs);
return(0);
}